/*
 *  Copyright (C) 2010-2012 VMware, Inc. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


dojo.declare("Login", wm.Page, {
    "preferredDevice": "desktop",
    start: function() {
        if (window["PhoneGap"]) {
            this.restorePhonegapCredentials();
        } else {
            this.usernameInput.setDataValue(dojo.cookie("user") || "");
            this.usernameInput.focus();
        }
        if (this.wmTitle) this.wmTitle.setCaption(app.name || app.declaredClass);
        this.loadingDialog.setMargin(parseInt(this.loadingDialog.widgetToCover.margin) + parseInt(this.loadingDialog.widgetToCover.border));
    },
    loginButtonClick: function(inSender) {
        this.usernameInput.setDataValue(this.usernameInput.getDataValue().toLowerCase());
        this.loginErrorMsg.setCaption("");
        dojo.cookie("user", this.usernameInput.getDataValue(), {
            expires: 365
        });
    },

    onLoginSuccess: function() {
        if (window["PhoneGap"]) {
            this.phonegapCredentialStorage.setData({
                name: this.usernameInput.getDataValue(),
                dataValue: this.passwordInput.getDataValue()
            });
        }
    },
    loginFailed: function(inResponse) {
        this.loginErrorMsg.setCaption("Invalid username or password.");
        this.usernameInput.focus();
    },
    restorePhonegapCredentials: function() {
        var username = this.phonegapCredentialStorage.getValue("name");
        var password = this.phonegapCredentialStorage.getValue("dataValue");
        if (username || password) {
            this.usernameInput.setDataValue(username);
            this.passwordInput.setDataValue(password);
            if (username && password) {
                this.loginVariable1.update();
            }
        }
    },
    svRegistrationSuccess2: function(inSender, inDeprecated) {

    },
    buttonRegistrationClick: function(inSender) {
        var isValid = true;

        if (this.luProjects.getDataValue() == undefined || this.luProjects.getDataValue() == "") {
            isValid = false;

        }
        if (this.emailEditor1.getDataValue() == undefined || this.emailEditor1.getDataValue() == "" || this.emailEditor1.getInvalid()) {
            isValid = false;
        }

        if (this.passwordEditor1.getDataValue() == undefined || this.passwordEditor1.getDataValue() == "") {
            isValid = false;
        }

        if (this.nameEditor1.getDataValue() == undefined || this.nameEditor1.getDataValue() == "") {
            isValid = false;
        }

        if (this.primaryPhoneEditor1.getDataValue() == undefined || this.primaryPhoneEditor1.getDataValue() == "" || this.primaryPhoneEditor1.getInvalid()) {
            isValid = false;
        }

        if (this.luDoor.dataValue == undefined || this.luDoor.dataValue == "") {
            isValid = false;
        }

        if (this.luBlock.dataValue == undefined || this.luBlock.dataValue == "") {
            isValid = false;
        }



        if (isValid) {
            this.usernameInput.setDataValue(this.emailEditor1.getDataValue());
            this.passwordInput.setDataValue(this.passwordEditor1.getDataValue());
            this.svRegistration.update();
        } else {

            this.ncRequiredFields.update();

        }

    },
    svRegistrationError: function(inSender, inError) {
        this.notifyRegistrationFail.input.setValue("text", inError);
        this.notifyRegistrationFail.update();
    },
    svRegistrationSuccess: function(inSender, inDeprecated) {


        this.usernameInput.setDataValue(this.emailEditor1.getDataValue());
        this.passwordInput.setDataValue(this.passwordEditor1.getDataValue());
        this.loginButton.click();
        this.formPanel1.clearData();
    },
    lvValidateActivationCodeResult: function(inSender, inDeprecated) {


        if (inSender.isEmpty()) {

            this.ncInValidActivationCode.update();

            //  this.textActivationCode.focus();
            this.buttonRegistration.setDisabled(true);
        } else {

            this.ncValidActivationCode.update();
            this.svBlocks.update();
            this.buttonRegistration.setDisabled(false);

        }

    },
    textActivationCodeChange: function(inSender, inDisplayValue, inDataValue, inSetByCode) {

        if (this.textActivationCode.isDirty) {
            this.lvValidateActivationCode.update();

        }

    },



    svVerifyUserResult: function(inSender, inDeprecated) {

        if (!inSender.isEmpty()) {
            var resultValue = inSender.getItem(0);
            if (resultValue.data.isActive) {
                //call the login variable
                this.loginVariable1.update();
            } else {
                //inactive user,don't let him login
                this.loginErrorMsg.setCaption("Your account is inactive.Please call customer care to reactivate it.");
                this.usernameInput.focus();
            }

        } else {
            //user desnt exist in our system,login will take care of it.
            this.loginVariable1.update();
        }



    },
    _end: 0
});